from django import forms
from django.core.exceptions import ValidationError
from .models import Patient


# from django.contrib.auth import get_user_model
# User = get_user_model()

class RegisterForm(forms.Form):
    phone = forms.CharField(max_length=50, min_length=1,
                            error_messages={
                                'required': '请输入手机号',
                                "max_length": '请输入正确的手机位数',
                                "min_length": '请输入正确的手机位数',

                            })
    password = forms.CharField(max_length=50, min_length=6,
                               error_messages={
                                   'required': '密码不能为空',
                                   "max_length": '密码位数过多',
                                   "min_length": '密码位数至少为6位',

                               })
    name = forms.CharField(max_length=50, min_length=1,                               error_messages={
                                   'required': '名字不能为空',
                                   "max_length": '名字过长',
                                   "min_length": '名字不能为空',

                               })
    gender = forms.CharField(max_length=50, min_length=1, error_messages={
                                    'required': '性别不能为空',
                                    "max_length": '性别过长',
                                    "min_length": '性别为空',
    })
    age = forms.IntegerField()

    def clean_phone(self):
        phone = self.cleaned_data.get('phone')
        print("验证手机号是否已经存在")
        print(Patient.objects.filter(phone=phone).exists())
        if Patient.objects.filter(phone=phone).exists():
            raise ValidationError('该手机号已被注册，请使用其他手机号')
        return phone
